package com.example.pei.ithinklearnt.view.customview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * 这个viewpager 用来解决左右滑动的时候 会拉出侧滑菜单栏
 * Created by Pei on 2018/4/13.
 */

public class MyViewPager1 extends ViewPager {



    //分别记录上次滑动的坐标
    private int mLastX=0;
    private int mLastY=0;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int x=(int)ev.getX();
        int y=(int)ev.getY();
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX=x-mLastX;
                int deltaY=y-mLastY;

                 if(Math.abs(deltaX)>Math.abs(deltaY)){
                    mLastX=x;
                    mLastY=y;
                    return true;
                }


            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        mLastX=x;
        mLastY=y;
        return super.onInterceptTouchEvent(ev);
    }


    public MyViewPager1(@NonNull Context context) {
        super(context);
    }

    public MyViewPager1(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
}
